package com.kunteng.ynhxksh.service;

import com.kunteng.ynhxksh.dao.JYTJDao;
import com.kunteng.ynhxksh.mapper.T_POS_JYXXMapper;
import com.kunteng.ynhxksh.util.DateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created By ShiYun with IntelliJ IDEA on 2018/4/2 10:45
 *
 * @author: ShiYun
 * @Email: 515867726@qq.com
 * @Date: 2018/4/2
 * To change this template use File | Settings | File Templates.
 */

/**
 * 交易统计服务
 */
@Service
public class JYTJService implements JYTJDao {

    Logger logger = LoggerFactory.getLogger(JYTJService.class);

    @Autowired
    private T_POS_JYXXMapper t_pos_jyxxMapper;

    @Override
    public List<Map> queryTodayJYZS() {
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("queryTodayJYZS {}", dateStr);
        List<Map> list = t_pos_jyxxMapper.queryJYZSByDate(dateStr);
        return list;
    }

    @Override
    public List<Map> queryTodayKPZS() {
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("queryTodayKPZS {}", dateStr);
        List<Map> list = t_pos_jyxxMapper.queryKPZSByDate(dateStr);
        return list;
    }

    @Override
    public JSONObject findTodayInfo() {
        JSONObject jsonObject = new JSONObject();
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("queryTodayJYKPInfo {}", dateStr);
        List<Map> jyxx = t_pos_jyxxMapper.queryJYByDate(dateStr);
        jsonObject.putAll(jyxx.get(0));
        List<Map> kpxx = t_pos_jyxxMapper.queryKPByDate(dateStr);
        jsonObject.putAll(kpxx.get(0));
        return jsonObject;
    }

    @Override
    public JSONArray queryLastKPEmoreThan1000() {
        JSONArray jsonArray = new JSONArray();
        logger.info("queryLastKPEmoreThan1000");
        List<Map> list = t_pos_jyxxMapper.queryLastKPEmoreThan1000();
        for (Map map : list) {
            JSONObject one = new JSONObject();
            one.put("qymc", "");
            one.put("hymc", "");
            one.put("kpje", "");
            one.put("kpsj", "");
            Object name = map.get("NAME");
            Object xfmc = map.get("XFMC");
            if (xfmc != null) {
                one.put("qymc", xfmc);
            }
            Object hymc = map.get("HYMC");
            if (hymc != null) {
                one.put("hymc", hymc);
            }
            Object jshj = map.get("JSHJ");
            if (jshj != null) {
                one.put("kpje", jshj);
            }
            Object kprq = map.get("KPRQ");
            if (kprq != null) {
                Date date = (Date) kprq;
                one.put("kpsj", DateUtils.getDate_yyyy_MM_dd_HH_mm_ss(date));
            }
            jsonArray.add(one);
        }
        return jsonArray;
    }

    @Override
    public JSONArray findTodayJYEInfoByQX() {
        JSONArray jsonArray = new JSONArray();
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("findTodayJYEInfoByQX {}", dateStr);
        List<Map> list = t_pos_jyxxMapper.queryJYByXZQHAndDate(dateStr);
        for (Map map : list) {
            JSONObject one = new JSONObject();
            one.put("xzqhdm", "");
            one.put("sjxzqhdm", "");
            one.put("xzqhmc", "");
            one.put("jye", "0");
            Object xzqh_dm = map.get("XZQH_DM");
            Object sjxzqh_dm = map.get("SJ_XZQH_DM");

            Object xzqh_mc = map.get("XZQH_MC");
            Object jyl = map.get("jyl");
            Object jye = map.get("jye");
            if (xzqh_dm != null) {
                one.put("xzqhdm", xzqh_dm);
            }
            if (sjxzqh_dm != null) {
                one.put("sjxzqhdm", sjxzqh_dm);
            }
            if (xzqh_mc != null) {
                one.put("xzqhmc", xzqh_mc);
            }
            if (jye != null) {
                one.put("jye", jye);
            }
            jsonArray.add(one);
        }
        return jsonArray;
    }

    @Override
    public List<Map> queryTodayJYETop5() {
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("queryTodayJYETop5 {}", dateStr);
        return t_pos_jyxxMapper.queryJYETopN(dateStr, 5);
    }

    @Override
    public List<Map> queryTodayKPETop5() {
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("queryTodayKPETop5 {}", dateStr);
        return t_pos_jyxxMapper.queryKPETopN(dateStr, 5);
    }

    @Override
    public JSONArray queryAWeekJYZS() {
        JSONArray jsonArray = new JSONArray();
        logger.info("queryAWeekJYZS");
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -i);
            String dateStr = DateUtils.getDate_yyyy_MM_dd(calendar.getTime());
            List<Map> list = t_pos_jyxxMapper.queryJYByDate(dateStr);
            JSONObject one = new JSONObject();
            one.put("date", dateStr);
            one.putAll(list.get(0));
            jsonArray.add(one);
        }
        return jsonArray;
    }

    @Override
    public JSONArray queryAWeekKPZS() {
        JSONArray jsonArray = new JSONArray();
        logger.info("queryAWeekKPZS");
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -i);
            String dateStr = DateUtils.getDate_yyyy_MM_dd(calendar.getTime());
            List<Map> list = t_pos_jyxxMapper.queryKPByDate(dateStr);
            JSONObject one = new JSONObject();
            one.put("date", dateStr);
            one.putAll(list.get(0));
            jsonArray.add(one);
        }
        return jsonArray;
    }

    @Override
    public JSONArray findDQJYE() {
        JSONArray jsonArray = new JSONArray();
        logger.info("findDQJYE");
        List<Map> list = t_pos_jyxxMapper.queryJYByXZQH();
        List<Map> total = t_pos_jyxxMapper.queryTotalJYE();
        //总交易额
        double total_jye = ((BigDecimal) total.get(0).get("jye")).doubleValue();
        for (Map map : list) {

            Object xzqh_dm = map.get("XZQH_DM");
            Object sjxzqh_dm = map.get("SJ_XZQH_DM");
            Object xzqh_mc = map.get("XZQH_MC");
            Object jyl = map.get("jyl");
            Object jye = map.get("jye");

            JSONObject one = new JSONObject();
            one.put("xzqhmc", "");
            one.put("jye", "0");
            one.put("zb", DateUtils.getBFB(0));
            if (xzqh_mc != null) {
                one.put("xzqhmc", xzqh_mc);
            }
            if (jye != null) {
                double a = ((BigDecimal) jye).doubleValue();
                one.put("jye", jye);
                one.put("zb", DateUtils.getBFB(a / total_jye));
            }
            jsonArray.add(one);
        }
        return jsonArray;
    }

    @Override
    public JSONArray findHYJYE() {
        JSONArray jsonArray = new JSONArray();
        logger.info("findHYJYE");
        List<Map> list = t_pos_jyxxMapper.queryJYByHY();
        List<Map> total = t_pos_jyxxMapper.queryTotalJYE();
        //总交易额
        double total_jye = ((BigDecimal) total.get(0).get("jye")).doubleValue();
        //前五个行业之外的合并到一块儿，为其他
        double other = total_jye;
        for (Map map : list) {
            JSONObject one = new JSONObject();
            Object hybm = map.get("HYBM");
            Object hymc = map.get("HYMC");
            double jye = ((BigDecimal) map.get("JYE")).doubleValue();
            one.put("hymc", "");
            if (hybm != null) {
                one.put("hymc", hymc);
            }
            one.put("jye", jye);
            String zb = DateUtils.getBFB(jye / total_jye);
            one.put("zb", zb);
            other -= jye;
            jsonArray.add(one);
        }
        JSONObject allOthers = new JSONObject();
        allOthers.put("hymc", "其他");
        allOthers.put("jye", other);
        allOthers.put("zb", DateUtils.getBFB(other / total_jye));
        jsonArray.add(allOthers);
        return jsonArray;
    }

}
